<cfcomponent extends="Model" output="false">

	<cffunction name="init">
		<cfset beforeSave("enterLadderDetails")>
		<cfset beforeDelete("removeLadderDetails")>
		<cfset belongsTo(name="Fixture",modelName="Fixture",foreignKey="fixtureID")>
	</cffunction>
	
	<cffunction name="enterLadderDetails" hint="This will add a row into the ladder">		
		<cfreturn addRemoveLadderEntry("add")>
	</cffunction>
	
	<cffunction name="removeLadderDetails">
		<cfreturn addRemoveLadderEntry("remove")>
	</cffunction>
	
	<!--- Make this function return a boolean back to the parent for validation purposes --->
	<cffunction name="addRemoveLadderEntry" returntype="boolean" hint="">
		<cfargument name="entryType" required="true" default="add">
		
		<cfset var result = false>
		<cfset getFixture = model("Fixture").findOne(where="FixtureID='#this.FixtureID#'")>
		
		<cfif isObject(getFixture)>	
			
			<!--- Enter Home Teams results into ladder --->
			<cfset homeTeam = {}>
			<cfset homeTeam.SeasonID = this.SeasonID>
			<cfset homeTeam.TeamID = getFixture.HomeTeamID>
			<cfset homeTeam.GamesFor = this.HomeTeamGames>
			<cfset homeTeam.GamesAgainst = this.AwayTeamGames>
			<cfset homeTeam.Points = this.HomeTeamPoints>
			
			<cfset result = singularLadderEntry(homeTeam,entryType)>
			
			
			<!--- Enter Away Teams results into ladder --->
			<cfset awayTeam = {}>
			<cfset awayTeam.SeasonID = this.SeasonID>
			<cfset awayTeam.TeamID = getFixture.AwayTeamID>
			<cfset awayTeam.GamesFor = this.AwayTeamGames>
			<cfset awayTeam.GamesAgainst = this.HomeTeamGames>
			<cfset awayTeam.Points = this.AwayTeamPoints>
			
			<cfset result = singularLadderEntry(awayTeam,entryType)>
			
			<!--- First lets take care of the home team --->
			<!---
			<cfset homeTeamLadder = model("Ladder").findOne(where="SeasonID='#this.seasonId#' AND TeamID='#getFixture.HomeTeamID#'")>
			
			<cfif isObject(homeTeamLadder)>
				<!--- Found existing --->
				
				<cfset homeTeamLadder.GamesFor = Evaluate(IIF(entryType EQ "add",DE("#homeTeamLadder.GamesFor#+#this.HomeTeamGames#"),DE("#homeTeamLadder.GamesFor#-#this.HomeTeamGames#")))>
				
				<cfset homeTeamLadder.GamesAgainst = Evaluate(IIF(entryType EQ "add",DE("#homeTeamLadder.GamesAgainst#+#this.HomeTeamGames#"),DE("#homeTeamLadder.GamesAgainst#-#this.AwayTeamGames#")))>
				
				<cfset homeTeamLadder.Points = Evaluate(IIF(entryType EQ "add",DE("#homeTeamLadder.Points#+#this.HomeTeamPoints#"),DE("#homeTeamLadder.Points#-#this.HomeTeamPoints#")))>
				
				<cfset homeTeamLadder.save()>
				<!--- 
				<cfset homeTeamLadder.GamesFor = homeTeamLadder.GamesFor + this.HomeTeamGames>
				<cfset homeTeamLadder.GamesAgainst = homeTeamLadder.GamesAgainst + this.AwayTeamGames>
				<cfset homeTeamLadder.Points = homeTeamLadder.Points + this.HomeTeamPoints>
				--->
			<cfelseif entryType NEQ "add" AND NOT isObject(homeTeamLadder)>
				<cfset homeTeamladder = model("Ladder").new()>
				<cfset homeTeamLadder.SeasonId = this.SeasonId>		
				<cfset homeTeamLadder.TeamId = getFixture.HomeTeamId>
				<cfset homeTeamLadder.GamesFor = this.HomeTeamGames>
				<cfset homeTeamLadder.GamesAgainst = this.AwayTeamGames>
				<cfset homeTeamLadder.Points = this.HomeTeamPoints>
				<cfset homeTeamLadder.save()>
			</cfif>
			
			
			
			<!--- Now do the away team --->
			<cfset awayTeamLadder = model("Ladder").findOne(where="SeasonID='#this.seasonId#' AND TeamID='#getFixture.AwayTeamID#'")>
			
			<cfif isObject(awayTeamLadder)>
				<!--- Found existing --->
				
				<cfset awayTeamLadder.GamesFor = Evaluate(IIF(entryType EQ "add",DE("#awayTeamLadder.GamesFor#+#this.HomeTeamGames#"),DE("#awayTeamLadder.GamesFor#-#this.AwayTeamGames#")))>
				
				<cfset awayTeamLadder.GamesAgainst = Evaluate(IIF(entryType EQ "add",DE("#awayTeamLadder.GamesAgainst#+#this.HomeTeamGames#"),DE("#awayTeamLadder.GamesAgainst#-#this.HomeTeamGames#")))>
				
				<cfset awayTeamLadder.Points = Evaluate(IIF(entryType EQ "add",DE("#awayTeamLadder.Points#+#this.HomeTeamGames#"),DE("#awayTeamLadder.Points#-#this.AwayTeamPoints#")))>
				<cfset awayTeamLadder.save()>
				<!---
				<cfset awayTeamLadder.GamesFor = awayTeamLadder.GamesFor + this.AwayTeamGames>
				<cfset awayTeamLadder.GamesAgainst = awayTeamLadder.GamesAgainst + this.HomeTeamGames>
				<cfset awayTeamLadder.Points = awayTeamLadder.Points + this.AwayTeamPoints>
				--->
			<cfelseif entryType NEQ "add" AND NOT isObject(awayTeamLadder)>
				<cfset awayTeamLadder = model("Ladder").new()>
				<cfset awayTeamLadder.SeasonId = this.SeasonId>		
				<cfset awayTeamLadder.TeamId = getFixture.AwayTeamId>
				<cfset awayTeamLadder.GamesFor = this.AwayTeamGames>
				<cfset awayTeamLadder.GamesAgainst = this.HomeTeamGames>
				<cfset awayTeamLadder.Points = this.AwayTeamPoints>			
				<cfset awayTeamLadder.save()>				
			</cfif>
			--->
			
		</cfif>
		
		<cfreturn result>
	</cffunction>
	
	
	<cffunction name="singularLadderEntry" returntype="boolean" hint="Enters one entry of a team into ladder">
		<cfargument name="newResult" type="struct" required="true">
		<cfargument name="entryType" type="string" required="true" default="add">
		
		<cfset ladderResult = model("Ladder").findOne(where="SeasonID='#newResult.SeasonID#' AND TeamID='#newResult.TeamID#'")>
		
		<cfif isObject(ladderResult)>
			<!--- Plus/Minus Games/Points from the ladder --->
			<cfset ladderResult.GamesFor = Evaluate(IIF(entryType EQ "add",
															DE("#ladderResult.GamesFor# + #newResult.GamesFor#"),
															DE("#ladderResult.GamesFor# - #newResult.GamesFor#")))>
															
			<cfset ladderResult.GamesAgainst = Evaluate(IIF(entryType EQ "add",
															DE("#ladderResult.GamesAgainst# + #newResult.GamesAgainst#"),
															DE("#ladderResult.GamesAgainst# - #newResult.GamesAgainst#")))>															
															
			<cfset ladderResult.Points = Evaluate(IIF(entryType EQ "add",
															DE("#ladderResult.Points# + #newResult.Points#"),
															DE("#ladderResult.Points# - #newResult.Points#")))>
			<cfreturn ladderResult.save()>		
		<cfelse>
			<cfif entryType EQ "add">
				<!--- Adding a new ladder entry --->
				<cfset ladderResult = model("Ladder").new(newResult)>	
				<cfreturn ladderResult.save()>
			<cfelse>
				<!--- Removing a blank entry --->
				<cfreturn true>
			</cfif>
		</cfif>	
		
	</cffunction>
	
	<cffunction name="getLadder" hint="Generates a ladder">
		<cfargument name="seasonId" type="numeric" default="">
		
		<cfset var result = ''>
		<cfquery name="result" datasource="#variables.wheels.class.connection.datasource#">
			SELECT * FROM Results r
			INNER JOIN Fixtures f ON r.FixtureID = f.FixtureID
			WHERE r.SeasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.seasonId#">
		</cfquery>
		
		<cfreturn result>		
	</cffunction>
	
	<cffunction name="getScorecard">
		<cfargument name="fixtureid" type="numeric" default="">
		
		<cfset var result = ''>
		
		<cfquery name="result" datasource="#variables.wheels.class.connection.datasource#">
			SELECT 
						pr.playerresultid,
						pr.fixtureid,
						pr.seasonid,
						pr.homeplayerid,
						pr.awayplayerid,
						pr.homeplayergames,
						pr.awayplayergames,
						pr.resulted,
						pr.created,
						hp.firstname "homeFirstName",
						hp.lastname "homeLastName",
						htp.position "homePosition",
						ap.firstname "awayFirstName",
						ap.lastname "awayLastName",
						atp.position "awayPosition"
			FROM 		playerresults pr
			INNER JOIN	players hp ON pr.homeplayerid = hp.playerid
			INNER JOIN	teamplayers htp ON pr.homeplayerid = htp.playerid
			INNER JOIN	players ap ON pr.awayplayerid = ap.playerid
			INNER JOIN	teamplayers atp ON pr.homeplayerid = atp.playerid
			WHERE 		pr.fixtureid = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.fixtureid#">
			ORDER BY	htp.position asc, atp.position asc
		</cfquery>
		
		<cfreturn result>
	</cffunction>

</cfcomponent>
